/*
 * @Author: Otway
 * @Date: 2019-01-09 11:16:08
 * @LastEditors: Otway
 * @LastEditTime: 2019-08-30 18:50:09
 * @copyright: h-visions
 */
import React, { PureComponent } from 'react';
import {
    Row,
    Col,
    Form,
    Input,
    Select,
} from 'antd';
const FormItem = Form.Item;
const { Option } = Select
const formItemLayout = {
    // 抽屉里面Form。二列的布局比例
    labelCol: {
        span: 6
    },
    wrapperCol: {
        span: 18
    }
};
class AddFrom extends PureComponent {
    state = {
        types: null,
        data: [],
        data1: {},
        data2: null,
        empty: false
    }
    // 挂载后
    componentDidMount() {
        console.log(this.props);
        const { one, formData } = this.props
        const { list } = this.props.CalssData[0]
        let groupLeader = {}
        this.props.CalssData[0].groupLeader.forEach((value, index) => {
            groupLeader[value.userName] = value.userId
        })
        if (one) {
            let data = []
            let data1 = {}
            list.forEach((value, index) => {
                if (value.factory === formData.factory) {
                    value.list.forEach((value_1, index_1) => {
                        data.push(value_1.workShop)
                        data1[value_1.workShop] = value_1.list
                    })
                }
            })
            this.setState({
                data,
                data1,
            })
        }
        this.setState({
            groupLeader
        })
    }
    // 判断选中显示隐藏
    selects = (value) => {
        console.log('当前类型选中', value);
        this.setState({
            types: this.state.types = value,
        })
    }

    onSelect = (LabeledValue) => {
        const { list } = this.props.CalssData[0]
        let data = []
        let data1 = {}
        list.forEach((value, index) => {
            if (value.factory === LabeledValue) {
                console.log(value.list);
                value.list.forEach((value_1, index_1) => {
                    data.push(value_1.workShop)
                    data1[value_1.workShop] = value_1.list
                })
            }
        })
        this.setState({
            data,
            data1,
            empty: true
        })
    }

    onChange = (a) => {
        const { setFieldsValue } = this.props.form
        const { list } = this.props.CalssData[0]
        let data = []
        let data1 = {}
        list.forEach((value, index) => {
            if (value.factory === a) {
                console.log(value.list);
                value.list.forEach((value_1, index_1) => {
                    data.push(value_1.workShop)
                    data1[value_1.workShop] = value_1.list
                })
            }
        })
        console.log(data, data1);
        this.setState({
            data,
            data1,
            empty: true
        })
        setFieldsValue({ workShop: undefined })
        setFieldsValue({ seat: undefined })
    }
    onChange1 = (a) => {
        const { setFieldsValue } = this.props.form
        const { data1 } = this.state
        this.setState({
            data2: data1[a]
        })
        setFieldsValue({ seat: undefined })
    }
    render() {
        const {
            formData,
            one,
            form: { getFieldDecorator },
            CalssData,
            userIds
        } = this.props;
        const { data, data2, empty } = this.state
        return (
            <Form >
                <Row>
                    <Col span={24}>
                        <FormItem {...formItemLayout} label={'集团:'}>
                            {getFieldDecorator('company', {
                                initialValue: CalssData[0].company,
                                rules: [
                                    {
                                        required: true,
                                        message: '集团为空'
                                    }
                                ],
                                getValueFromEvent: (event) => {
                                    return event.target.value.replace(/\s+/g, "")
                                },
                            })(<Input placeholder='输入集团' allowClear maxLength={20} disabled={true} />)}
                        </FormItem>
                    </Col>
                    <Col span={24}>
                        <FormItem {...formItemLayout} label={'工厂:'}>
                            {getFieldDecorator('factory', {
                                initialValue: one ? formData.factory : '',
                                rules: [
                                    {
                                        required: true,
                                        message: '工厂不能为空'
                                    }
                                ]
                            })(<Select allowClear={true} onChange={this.onChange} onSelect={this.onSelect}>
                                {
                                    CalssData[0] ? CalssData[0].list.map((value, index) => {
                                        return <Option value={value.factory} key={`factory_${index}`}>{value.factory}</Option>
                                    }) : null
                                }
                            </Select>)}
                        </FormItem>
                    </Col>
                    <Col span={24}>
                        <FormItem {...formItemLayout} label={'所属车间:'}>
                            {getFieldDecorator('workShop', {
                                initialValue: one ? (!empty ? formData.workShop : null) : null,
                                setFieldsValue: { workShop: undefined },
                                rules: [
                                    {
                                        required: true,
                                        message: '所属车间不能为空'
                                    }
                                ]
                            })(<Select allowClear={true} onChange={this.onChange1}>
                                {
                                    data !== [] ? data.map((value, index) => {
                                        return <Option value={value} key={`workShop_${index}`}>{value}</Option>
                                    }) : null
                                }
                            </Select>)}
                        </FormItem>
                    </Col>
                    <Col span={24}>
                        <FormItem {...formItemLayout} label={'班组编码:'}>
                            {getFieldDecorator('groupCode', {
                                initialValue: one ? formData.groupCode : null,
                                rules: [
                                    {
                                        required: true,
                                        message: '班组编码不能为空'
                                    }
                                ],
                                getValueFromEvent: (event) => {
                                    return event.target.value.replace(/\s+/g, "")
                                },
                            })(
                                <Input placeholder='请输入班组编码' maxLength={20} allowClear />
                            )}
                        </FormItem>
                    </Col>
                    <Col span={24}>
                        <FormItem {...formItemLayout} label={'班组名称:'}>
                            {getFieldDecorator('groupName', {
                                initialValue: one ? formData.groupName : null,
                                rules: [
                                    {
                                        required: true,
                                        message: '班组名称不能为空'
                                    }
                                ],
                                getValueFromEvent: (event) => {
                                    return event.target.value.replace(/\s+/g, "")
                                },
                            })(
                                <Input placeholder='请输入班组名称' maxLength={20} allowClear />
                            )}
                        </FormItem>
                    </Col>
                    <Col span={24}>
                        <FormItem {...formItemLayout} label={'班组类型:'}>
                            {getFieldDecorator('groupType', {
                                initialValue: one ? formData.groupType : null,
                                rules: [
                                    {
                                        required: true,
                                        message: '班组类型不能为空'
                                    }
                                ],
                                getValueFromEvent: (event) => {
                                    return event.target.value.replace(/\s+/g, "")
                                },
                            })(
                                <Input placeholder='请输入班组类型' maxLength={20} allowClear />
                            )}
                        </FormItem>
                    </Col>
                    <Col span={24}>
                        <FormItem {...formItemLayout} label={'默认机位:'}>
                            {getFieldDecorator('seat', {
                                initialValue: one ? (!empty ? formData.seat : null) : null,
                                rules: [
                                    {
                                        required: true,
                                        message: '默认机位为空'
                                    }
                                ]
                            })(<Select allowClear={true}>
                                {
                                    data2 !== null && data2 !== undefined ? data2.map((value, index) => {
                                        return <Option value={value.placement} key={`workShop_${index}`}>{value.placement}</Option>
                                    }) : null
                                }
                            </Select>)}
                        </FormItem>
                    </Col>
                    <Col span={24}>
                        <FormItem {...formItemLayout} label={'班组长:'}>
                            {getFieldDecorator('groupLeader', {
                                initialValue: one ? formData.groupLeader : null,
                                rules: [
                                    {
                                        required: true,
                                        message: '班组长为空'
                                    }
                                ]
                            })(
                                <Select allowClear={true} onChange={(e) => {
                                    userIds(this.state.groupLeader[e])
                                }}>
                                    {
                                        CalssData[0] ? CalssData[0].groupLeader.map((value, index) => {
                                            return <Option value={value.userName} key={value.userId}>{value.userName}</Option>
                                        }) : null
                                    }
                                </Select>
                            )}
                        </FormItem>
                    </Col>

                    <Col span={24}>
                        <FormItem {...formItemLayout} label={'状态:'}>
                            {getFieldDecorator('isDelete', {
                                initialValue: one ? formData.isDelete : null,
                                rules: [
                                    {
                                        required: true,
                                        message: '状态为空'
                                    }
                                ]
                            })(
                                <Select allowClear={true}>
                                    <Option value={0}>启用</Option>
                                    <Option value={1}>停用</Option>
                                </Select>
                            )}
                        </FormItem>
                    </Col>
                </Row>
            </Form>
        );
    }
}

export default Form.create()(AddFrom);
